get_user = $.ajax({
    type: "get",
    url: "../php/cookie_check.php",
    data: {
        cd: $.cookie('cd')
    },
    async: false,
    dataType: "json",
    success: function (response) {
        if (response == '0') {
            window.location.href = '../login.html'
        } else {
            user_name = response.user;
            nickname=response.nickname
            url=response.url
            admin=response.admin
            if(admin==1){
                $('#name').html(nickname+"(管理员)");
                $("#fill").html('创建表单');
                $("#fill").attr('id',"create");
            }
            else{
                $('#name').html(nickname);
            }
        }
    }
});

$.ajax({
    type: "get",
    url: url,
    complete: function(xhr, textStatus) {
        if(xhr.status==404){

        }else{
        $("#head").css('background', 'url('+url+')');
        $("#head").css("background-size",'cover');
        }
}});
$("#create").click(function (e) { 
    e.preventDefault();
    window.location.href='form_create.html'
});
$('#nickname').click(function (e) {
    e.preventDefault();
    swal({
        closeOnEsc: false,
        showCancelButton: true,
        closeOnClickOutside: false,
        buttons: ['取消', '提交'],
        content: {
            element: "input",
            attributes: {
                id: 'new',
                placeholder: "新昵称:",
                type: "text",
                autocomplete: "off",
            },
        },
    }).then((value) => {
        if (value != null && value != '') {
            let patter_special_char = /[`~!@#$%^&*()_+<>?:"{},.\/;'[\]·！#￥（——）：；“”‘、，|《。》？、【】[\]]/;
            let match = patter_special_char.test(value);
            if (!match) {
                new_nickname = value
                $.ajax({
                    type: "get",
                    url: "../php/nick_name_reset.php",
                    data: {
                        nickname: nickname,
                        new_nickname: value,
                    },
                    success: function (response) {
                        if (response == '0') {
                            swal({
                                icon: 'error',
                                text: '昵称更改失败！'
                            })
                        } else {
                            swal({
                                icon: 'success',
                                text: '昵称更改成功,新昵称为'+response+'。',
                                buttons: '确定',
                            }).then(value => {
                                switch (value) {
                                    case true:
                                        $("#name").html(new_nickname);
                                }
                            })
                        }
                    }
                });
            } else {
                swal({
                    icon: 'error',
                    text: '昵称不能含有特殊字符或为空！'
                })
            }
        } else {
            if (value == '')
                swal({
                    icon: 'error',
                    text: '昵称不能为空！'
                })
        }


    });
});
$("#logout").click(function (e) {
    console.log('1');
    e.preventDefault();
    $.cookie('cd', '', {
        expires: -1,
        path: '/',
    })
    window.location.href = '../login.html'
});
$('#upl').click(function (e) {
    e.preventDefault();
    $('#total_upload').removeClass('hide')
});
var options = {
    // 初始化选项
    autoUpload:true,
    limitFilesCount: 1,
    url: "../php/face_reset.php",
    limitFilesCount: 1,
    multi_selection: false,
    sendFileName:false,
    resize:{
        
            width: 128,
            height: 128,
            crop: false,
            quuality: 90,
            preserve_headers: false
        
    },
    filters: { // 只允许上传图片或图标（.ico）
        mime_types: [{  
                title: '图片',
                extensions: 'jpg,gif,png'
            },
            {
                title: '图标',
                extensions: 'ico'
            }
        ],
        // 最大上传文件为 2MB
        // max_file_size: '2mb',
    },
    removeUploaded:true,
    uploadedMessage: function (result) {
        console.log(result)
        if(result.failed!=0){
            swal({
                icon: 'error',
                text: '上传失败',
            })
        }else{
            swal({
                icon: 'success',
                text: '成功上传!',
            }).then(function(){
                location.reload()            })
        }
        $('#total_upload').addClass('hide')
    }
};
$('.btn-cancle').click(function (e) { 
    e.preventDefault();
    $('#total_upload').addClass('hide')
});
$('#uploader').uploader(options)